#!/usr/bin/env bash
set -e

if [[ ("$1" = "kinetic" || "$1" = "melodic" ) && ("$2" = "synquacer" || "$2" = "driveworks" || "$2" = "generic-aarch64") ]]
then
    AUTOWARE_TARGET_ARCH=aarch64
    AUTOWARE_TARGET_ROS_DISTRO=$1
    AUTOWARE_TARGET_PLATFORM=$2
    shift 2

   if [[ -d build-${AUTOWARE_TARGET_PLATFORM} ]]; then
       rm -rf build-${AUTOWARE_TARGET_PLATFORM}
   fi

   if [[ -d install-${AUTOWARE_TARGET_PLATFORM} ]]; then
       rm -rf install-${AUTOWARE_TARGET_PLATFORM}
   fi

    AUTOWARE_HOME=/home/autoware/Autoware
    if [ "$AUTOWARE_TARGET_PLATFORM" = "driveworks" ]
    then
        if [[ -z "${DRIVEWORKS_TOOLKIT_ROOT_DIR}" ]] || [[ -z "${CUDA_TOOLKIT_ROOT_DIR}" ]] || [[ -z "${NVIDIA_TENSORRT_ROOT_DIR}" ]] || [[ -z "${DRIVE_T186REF_LINUX_ROOT_DIR}" ]]
        then
            echo "Please ensure that the following environment variables are set and point to their respective correct locations: DRIVEWORKS_TOOLKIT_ROOT_DIR CUDA_TOOLKIT_ROOT_DIR NVIDIA_TENSORRT_ROOT_DIR DRIVE_T186REF_LINUX_ROOT_DIR"
            exit 1
        fi
        AUTOWARE_HOME=/home/nvidia/Autoware
        AUTOWARE_TOOLCHAIN_FILE=cross_toolchain_driveworks.cmake
        AUTOWARE_DOCKER_EXTRA_ARGS=$(cat <<-END
-v ${DRIVEWORKS_TOOLKIT_ROOT_DIR}:/usr/local/driveworks:ro
-v ${CUDA_TOOLKIT_ROOT_DIR}:/usr/local/cuda:ro
-v ${NVIDIA_TENSORRT_ROOT_DIR}:/usr/local/tensorrt:ro
-v ${DRIVE_T186REF_LINUX_ROOT_DIR}:/usr/local/drive-t186ref-linux:ro
-e DRIVEWORKS_TOOLKIT_ROOT_DIR=/usr/local/driveworks
-e CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda
-e NVIDIA_TENSORRT_ROOT_DIR=/usr/local/tensorrt
-e DRIVE_T186REF_LINUX_ROOT_DIR=/usr/local/drive-t186ref-linux
-e AUTOWARE_CYTHON_EXECUTABLE=/usr/bin/cython
-e AUTOWARE_CUDA_ARCH=sm_61
END
)
        AUTOWARE_CUDA_ARCH=sm_61
    else
        AUTOWARE_TOOLCHAIN_FILE=cross_toolchain.cmake
    fi
    if [ ! -f "${PWD}/${AUTOWARE_TOOLCHAIN_FILE}" ]; then
        echo "Toolchain file: '${PWD}/${AUTOWARE_TOOLCHAIN_FILE}' not found"
        exit 1
    fi

    AUTOWARE_BUILD_PATH=${AUTOWARE_HOME}/ros/build-${AUTOWARE_TARGET_PLATFORM}
    AUTOWARE_INSTALL_PATH=${AUTOWARE_HOME}/ros/install-${AUTOWARE_TARGET_PLATFORM}

    AUTOWARE_TOOLCHAIN_FILE_PATH="${AUTOWARE_HOME}/ros/${AUTOWARE_TOOLCHAIN_FILE}"
    echo "Using toolchain file: '${AUTOWARE_TOOLCHAIN_FILE_PATH}''"
    AUTOWARE_SYSROOT=/sysroot/${AUTOWARE_TARGET_PLATFORM}

    AUTOWARE_DOCKER_DATE="latest"

    docker container run \
        -it \
        --rm \
        ${AUTOWARE_DOCKER_EXTRA_ARGS} \
        -e AUTOWARE_SYSROOT=${AUTOWARE_SYSROOT} \
        -v ${PWD}/../:${AUTOWARE_HOME} \
        -w ${AUTOWARE_HOME}/ros \
        -u ${UID}:${UID} \
        autoware/build:${AUTOWARE_TARGET_PLATFORM}-${AUTOWARE_TARGET_ROS_DISTRO}-${AUTOWARE_DOCKER_DATE} \
        bash \
        -c "\
        source ${AUTOWARE_SYSROOT}/opt/ros/${AUTOWARE_TARGET_ROS_DISTRO}/setup.bash && \
        colcon build \
            --merge-install \
            --build-base ${AUTOWARE_BUILD_PATH} \
            --install-base ${AUTOWARE_INSTALL_PATH} \
            --packages-skip ymc \
            --cmake-args \
            -DCYTHON_EXECUTABLE=/usr/bin/cython \
            -DCUDA_ARCH=${AUTOWARE_CUDA_ARCH} \
            -DCMAKE_TOOLCHAIN_FILE=${AUTOWARE_TOOLCHAIN_FILE_PATH} \
            -DCMAKE_SYSTEM_PROCESSOR=${AUTOWARE_TARGET_ARCH} \
            -DCMAKE_PREFIX_PATH='${AUTOWARE_SYSROOT}/opt/ros/${AUTOWARE_TARGET_ROS_DISTRO};${AUTOWARE_INSTALL_PATH}' \
            -DCMAKE_FIND_ROOT_PATH=${AUTOWARE_INSTALL_PATH} \
            -DCMAKE_BUILD_TYPE=Release $*
        "

    find install-${AUTOWARE_TARGET_PLATFORM}/ -type f -exec grep -Iq . {} \; -and -exec sed -i -e "s#${AUTOWARE_SYSROOT}##g" {} \;
else
  echo "Select target ROS distribution (kinetic, melodic) and target platform (synquacer, driveworks, generic-aarch64)"
fi

